package com.msb.tank;

import java.awt.*;
import java.util.Random;

/**
 * @Author: chshh
 * @version: 1.0
 */
public class Tank {

    private int x,y;
    public static final int WIDTH = ResourceMgr.goodTankL.getWidth(),HEIGHT = ResourceMgr.goodTankU.getHeight();
    private Direction dir = Direction.DOWN;
    private static final int SPEED = 4;
    private boolean moving = true;
    private boolean living = true;
    private TankFrame tf = null;
    private Random random = new Random();
    private Group group = Group.BAD;

    Rectangle rect = new Rectangle();

    public Tank(int x, int y, Direction dir,Group group,TankFrame tf) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.group = group;
        this.tf = tf;

        rect.x = this.x;
        rect.y = this.y;
        rect.width = WIDTH;
        rect.height = HEIGHT;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Direction getDir() {
        return dir;
    }

    public void setDir(Direction dir) {
        this.dir = dir;
    }

    public void paint(Graphics g) {
        if (!living) tf.tanks.remove(this);

        switch (dir){
            case LEFT:
                g.drawImage(this.group == Group.GOOD?ResourceMgr.goodTankL:ResourceMgr.badTankL,x,y,null);
                break;
            case UP:
                g.drawImage(this.group == Group.GOOD?ResourceMgr.goodTankU:ResourceMgr.badTankU,x,y,null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD?ResourceMgr.goodTankR:ResourceMgr.badTankR,x,y,null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD?ResourceMgr.goodTankD:ResourceMgr.badTankD,x,y,null);
                break;
        }
        move();
    }

    private void move() {
        if (!moving) return ;
        switch (dir){
            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
        if (this.group == Group.BAD && random.nextInt(100) > 95 ) this.fire();

        if (this.group == Group.BAD && random.nextInt(100) > 95 ) randomDir();

        boundCheck();

        rect.x = this.x;
        rect.y = this.y;
    }

    //边界检测
    private void boundCheck() {
        if (this.x <2) x=2;
        if (this.y <28) y=28;
        if (this.x >TankFrame.GAME_WIDTH - Tank.WIDTH -2) x= TankFrame.GAME_WIDTH - Tank.WIDTH -2;
        if (this.y >TankFrame.GAME_HEIGHT - Tank.HEIGHT -2) y= TankFrame.GAME_HEIGHT - Tank.HEIGHT -2;
    }

    //随机发射方向
    private void randomDir() {
        this.dir = Direction.values()[random.nextInt(4)];
    }

    public void fire() {
        int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
        int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;

        tf.bullets.add(new Bullet(bX,bY,this.dir,this.group,this.tf));

    }

    public void die() {
        this.living = false;
    }
}
